import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;

public class Excercises {
	HashMap<String, Group> groups;
	HashMap<String, Tutor> tutors;
	HashMap<Student, Mark> finalMarks;

	public Excercises(HashMap<String, Group> groups, HashMap<String, Tutor> tutors) {
		this.groups = groups;
		this.tutors = tutors;
	}
	
	public Excercises(String baseId, int number, int maxStudents) {
		this.addGroups(baseId, number, maxStudents);
	}
	
	public Excercises(String baseId, int number) {
		this.addGroups(baseId, number);
	}
	
	public Excercises(Course course, int number, int maxStudents) {
		this.addGroups(course, number, maxStudents);
	}
	
	public Excercises(Course course, int number) {
		this.addGroups(course, number);
	}
	
	public Excercises() {
	}
	
	public void addGroup(String id, Tutor tutor, int maxStudents) {
		groups.put(id, new Group(id, tutor, maxStudents));
	}
	
	public void addGroup(Group group) {
		groups.put(group.getID(), group);
	}
	
	public void addGroups(String baseId, int number, int maxStudents) {
		for (int i = 0; i < number; i++) {
			groups.put(baseId+i, new Group(baseId+i, maxStudents));
		}
	}
	
	public void addGroups(String baseId, int number) {
		for (int i = 0; i < number; i++) {
			groups.put(baseId+i, new Group(baseId+i));
		}
	}
	
	public void addGroups(Course course, int number, int maxStudents) {
		for (int i = 0; i < number; i++) {
			groups.put(course.getLvanr()+i, new Group(course.getLvanr()+i, maxStudents));
		}
	}
	
	public void addGroups(Course course, int number) {
		for (int i = 0; i < number; i++) {
			groups.put(course.getLvanr()+i, new Group(course.getLvanr()+i));
		}
	}
	
	public boolean addTutor(Tutor tutor) {
		if (!this.tutors.containsKey(tutor.getId())) {
			this.tutors.put(tutor.getId(), tutor);
			return true;
		}
		return false;
	}
	
	public boolean removeTutor(Tutor tutor) {
		if (this.tutors.containsKey(tutor.getId())) {
			this.tutors.remove(tutor.getId());
			Iterator it = groups.keySet().iterator();
			while(it.hasNext()) {
				String key = (String)it.next();
				Group group = groups.get(key);
				group.removeTutor(tutor);
			}
			return true;
		}
		return false;
	}
	
	public boolean swapTutor(Tutor tutor1, Tutor tutor2) {
		if (this.tutors.containsKey(tutor1.getId())) {
			this.tutors.remove(tutor1.getId());
			this.tutors.put(tutor2.getId(), tutor2);
			Iterator it = groups.keySet().iterator();
			while(it.hasNext()) {
				String key = (String)it.next();
				Group group = groups.get(key);
				group.swapTutor(tutor1, tutor2);
			}
			return true;
		}
		return false;
	}
	
	public boolean addStudentToGroup(Student student, Group group) {
		return group.addStudent(student);
	}
	
	public boolean removeStudentFromGroup(Student student, Group group) {
		return group.removeStudent(student);
	}
	
	public void giveFinalMark(Student student, int mark) {
		this.finalMarks.put(student, Mark.createMark(mark));
	}
	
	public void giveFinalMark(Student student, Mark mark) {
		this.finalMarks.put(student, mark);
	}
	
}
